package online.misscl.lsy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import online.misscl.lsy.entity.HospitalInformation;
import online.misscl.lsy.entity.VaccineSort;
import online.misscl.lsy.entity.VaccineSortHospital;
import online.misscl.lsy.entity.dto.GetVaccineInfoDTO;
import online.misscl.lsy.entity.dto.VaccineSortQueryDTO;
import online.misscl.lsy.entity.dto.VaccineSortUpdateDTO;
import online.misscl.lsy.entity.dto.vo.VaccineInformationVo;
import online.misscl.lsy.entity.dto.vo.VaccineSortVo;
import online.misscl.lsy.mapper.VaccineSortHospitalMapper;
import online.misscl.lsy.mapper.VaccineSortMapper;
import online.misscl.lsy.service.VaccineSortService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * <p>
 * 疫苗分类 服务实现类
 * </p>
 *
 * @author misscl
 * @since 2023/03/11 11:14
 */
@Service
public class VaccineSortServiceImpl extends ServiceImpl<VaccineSortMapper, VaccineSort> implements VaccineSortService {

    @Autowired
    VaccineSortHospitalMapper vaccineSortHospitalMapper;

    @Override
    public List<VaccineSortVo> getVaccineSort() {
        MPJLambdaWrapper<VaccineSort> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.select(VaccineSort::getVaccineSortId).distinct().select(VaccineSort::getVaccineSortName);
        return baseMapper.selectJoinList(VaccineSortVo.class, mpjLambdaWrapper);
    }

//
//    @Override
//    public List<HospitalInformation> getHospitalBySort(String sort) {
//        MPJLambdaWrapper<HospitalInformation> hospitalInformationMPJLambdaWrapper = new MPJLambdaWrapper<>();
//        hospitalInformationMPJLambdaWrapper.select(HospitalInformation::getHospitalId,HospitalInformation::getHospitalAddress,
//                HospitalInformation::getHospitalName,HospitalInformation::getHospitalPhone)
//                .leftJoin(HospitalInformation.class,HospitalInformation::getHospitalId,VaccineSort::g)
//                .groupBy(HospitalInformation::getHospitalId)
//                .eq(sort != null,VaccineSort::getVaccineSort,sort);
////                                            .eq(HospitalInformation::getHospitalId,1);
//        return baseMapper.selectJoinList(HospitalInformation.class,hospitalInformationMPJLambdaWrapper);
//        return null;
//    }



    @Override
    public List<VaccineInformationVo> getVaccineInfo(GetVaccineInfoDTO dto) {
//        MPJLambdaWrapper<VaccineInformation> mpjLambdaWrapper = new MPJLambdaWrapper<>();
//        mpjLambdaWrapper.select(VaccineInformation::getVaccineId,VaccineInformation::getVaccineName,VaccineInformation::getVaccinePrice,
//                VaccineInformation::getVaccineFactory,VaccineInformation::getVaccineType,VaccineInformation::getVaccineIntro,VaccineInformation::getVaccineImg,
//                VaccineInformation::getSuitableAge)
//                .leftJoin(VaccineInformation.class, VaccineInformation::getVaccTypeId,VaccineSort::getVaccineSortId)
//                .select(VaccineInformation::getVaccTypeId)
//                .eq(VaccineSort::getHospitalId,dto.getHospitalId())
//                .eq(dto.getVaccineId() != null,VaccineInformation::getVaccineId,dto.getVaccineId())
//                .eq(dto.getSortId() != null,VaccineSort::getVaccineSortId,dto.getSortId())
//                .orderBy(true,true,VaccineInformation::getVaccineId);
//        return baseMapper.selectJoinList(VaccineInformationVo.class,mpjLambdaWrapper);
    return null;
    }

    @Override
    public List<VaccineSortVo> vaccineSortList(VaccineSortQueryDTO vaccineSortQueryDTO) {
        //         <VaccineSort> wrapper = new LambdaQueryWrapper<>();
//        MPJLambdaWrapper<VaccineSort> mpjLambdaWrapper = new MPJLambdaWrapper<VaccineSort>();
////        wrapper.select(VaccineSort::getVaccineSort,HospitalInformation::getHospitalName)
//        mpjLambdaWrapper
//                .distinct()
//                .select(VaccineSort::getVaccineSort)
//                .leftJoin(HospitalInformation.class,HospitalInformation::getHospitalId,VaccineSort::getHospitalId)
//                .select(HospitalInformation::getHospitalName)
//                .eq(vaccineSortQueryDTO.getHospitalId()!=null,HospitalInformation::getHospitalName,vaccineSortQueryDTO.getHospitalId())
//                .like(vaccineSortQueryDTO.getVaccineSort()!=null,VaccineSort::getVaccineSort,vaccineSortQueryDTO.getVaccineSort())
//                .like(vaccineSortQueryDTO.getHospitalName()!=null,HospitalInformation::getHospitalName,vaccineSortQueryDTO.getHospitalName())
//        ;
//        return baseMapper.selectJoinList(VaccineSortVo.class,mpjLambdaWrapper);
    return null;
    }


    @Override
    public List<VaccineSortVo> vaccineSortQueryList(VaccineSortQueryDTO vaccineSortQueryDTO) {
        MPJLambdaWrapper<VaccineSort> mpjLambdaWrapper = new MPJLambdaWrapper<VaccineSort>();
//        wrapper.select(VaccineSort::getVaccineSort,HospitalInformation::getHospitalName)
        mpjLambdaWrapper
                .distinct()
                .select(VaccineSort::getVaccineSortName)
                .leftJoin(VaccineSortHospital.class,VaccineSortHospital::getVaccineSortId,VaccineSort::getVaccineSortId)
                .selectAll(VaccineSortHospital.class)
                .leftJoin(HospitalInformation.class,HospitalInformation::getHospitalId,VaccineSortHospital::getHospitalId)
                .select(HospitalInformation::getHospitalName)
                .eq(vaccineSortQueryDTO.getHospitalId()!=null,HospitalInformation::getHospitalId,vaccineSortQueryDTO.getHospitalId())
                .like(vaccineSortQueryDTO.getVaccineSortName()!=null,VaccineSort::getVaccineSortName,vaccineSortQueryDTO.getVaccineSortName())
                .like(vaccineSortQueryDTO.getHospitalName()!=null,HospitalInformation::getHospitalName,vaccineSortQueryDTO.getHospitalName())
        ;

        return baseMapper.selectJoinList(VaccineSortVo.class,mpjLambdaWrapper);
    }

    @Override
    public VaccineSortVo getOneVaccineSortVo(Integer vshId) {
        MPJLambdaWrapper<VaccineSort> mpjLambdaWrapper = new MPJLambdaWrapper<VaccineSort>();
//        wrapper.select(VaccineSort::getVaccineSort,HospitalInformation::getHospitalName)
        mpjLambdaWrapper
                .distinct()

                .select(VaccineSort::getVaccineSortName)
                .leftJoin(VaccineSortHospital.class,VaccineSortHospital::getVaccineSortId,VaccineSort::getVaccineSortId)
                .selectAll(VaccineSortHospital.class)

                .leftJoin(HospitalInformation.class,HospitalInformation::getHospitalId,VaccineSortHospital::getHospitalId)
                .select(HospitalInformation::getHospitalName)
                .eq(VaccineSortHospital::getId,vshId)
;
        return baseMapper.selectJoinOne(VaccineSortVo.class,mpjLambdaWrapper);
    }

    @Override
    public int deleteVaccineSort(Integer userId) {

        return baseMapper.deleteById(userId);
    }

    @Override
    public int updateVaccineSort(VaccineSortHospital vaccineSortHospital) {
//        VaccineSort vaccineSort =new VaccineSort();
//        BeanUtils.copyProperties(vaccineSortUpdateDTO,vaccineSort);
        return vaccineSortHospitalMapper.updateById(vaccineSortHospital);
    }

    @Override
    public int saveVaccineSort(VaccineSortHospital vaccineSortHospital) {
        return vaccineSortHospitalMapper.insert(vaccineSortHospital);
    }
    /**
     * 新增疫苗分类前的重名检查
     *
     * 若已经在关联表中存在则返回 0
     * 不重复则返回分类字典中的id
     * @param vaccineSortUpdateDTO
     * @return
     */
    @Override
    public Integer getVaccineSortId(VaccineSortUpdateDTO vaccineSortUpdateDTO) {
//        先查询分类字典获取对应id 若不存在则添加新的分类字典，并返回true
        LambdaQueryWrapper<VaccineSort> vaccineSortLambdaQueryWrapper = new LambdaQueryWrapper<VaccineSort>();
        vaccineSortLambdaQueryWrapper.select(VaccineSort::getVaccineSortId)
                .eq(VaccineSort::getVaccineSortName,vaccineSortUpdateDTO.getVaccineSortName());
        VaccineSort vs = baseMapper.selectOne(vaccineSortLambdaQueryWrapper);
//        分类字典中不存在，则添加分类字典

        if(vs==null){
            VaccineSort insert = new VaccineSort();
            insert.setVaccineSortName(vaccineSortUpdateDTO.getVaccineSortName());
            baseMapper.insert(insert);
            return  insert.getVaccineSortId();
        }

//        在分类和医院的关联表中查重，重复则返回0
        LambdaQueryWrapper<VaccineSortHospital> lq = new LambdaQueryWrapper<VaccineSortHospital>();
        lq.select(VaccineSortHospital::getId)
                .eq(VaccineSortHospital::getHospitalId,vaccineSortUpdateDTO.getHospitalId())
                .eq(VaccineSortHospital::getVaccineSortId,vs.getVaccineSortId())
        ;
//        重复了，返回0
        if(vaccineSortHospitalMapper.selectCount(lq)!=0)
            return 0;
//        未重复，将分类字典的id返回
        return vs.getVaccineSortId();
    }

}
